package com.cys.markdown.doc.admin.controller.sys;

import com.cys.common.data.RespCodeEnum;
import com.cys.common.pojo.RespEntity;
import com.cys.common.util.base.MapUtil;
import com.cys.common.util.base.StringUtil;
import com.cys.mid.core.system.entity.SysRight;
import com.cys.mid.core.system.interfaces.entity.ISysRight;
import com.cys.mid.core.system.interfaces.service.SysRightService;
import com.cys.mid.core.system.interfaces.service.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限Controller
 *
 * @author caiyushen
 */
@Controller
@RequestMapping("/admin/sys/right")
@Transactional(rollbackFor = Exception.class)
public class RightController {

    private final static Logger logger = LoggerFactory.getLogger(RightController.class);

    @Resource(name = "sysRightServiceImpl")
    private SysRightService sysRightService;

    @Resource(name = "sysRoleServiceImpl")
    private SysRoleService sysRoleService;


    /**
     * 获取列表信息
     *
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public RespEntity getList() {
        List<ISysRight> treeList = sysRightService.findTreeList();
        return new RespEntity(RespCodeEnum.success, treeList);
    }

    /**
     * 获取树形列表信息
     *
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/getTreeList", method = RequestMethod.GET)
    public RespEntity getTreeList() {
        List<ISysRight> treeList = sysRightService.findTreeList();
        return new RespEntity(RespCodeEnum.success, treeList);
    }


    /**
     * 新增Privilege
     *
     * @param param privilege数据集
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RespEntity add(@RequestParam Map param) throws Exception {
        logger.debug("method[add] param: " + param.toString());
        Long sysRightParentId = MapUtil.getLong(param, "sysRightParentId");
        if (0L == sysRightParentId) {
            param.put("sysRightParentId", null);
        }
        sysRightService.save(param);
        return new RespEntity(RespCodeEnum.success);
    }

    /**
     * 根据id获取权限数据
     *
     * @param sysRightId 权限主键
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public RespEntity find(Long sysRightId) {
        if (!StringUtil.checkObj(sysRightId)) {
            logger.warn("method[find] sysRightId is null.");
            return new RespEntity(RespCodeEnum.deficiency);
        }
        ISysRight systemRight = sysRightService.find(sysRightId);
        return new RespEntity(RespCodeEnum.success, systemRight);
    }

    /**
     * 修改Privilege
     *
     * @param param privilege数据集
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public RespEntity update(@RequestParam Map param) throws Exception {
        logger.debug("method[update] param: " + param.toString());
        Long privilegeParentId = MapUtil.getLong(param, "sysRightParentId");
        if (0L == privilegeParentId) {
            param.put("sysRightParentId", null);
        }
        sysRightService.update(param);
        return new RespEntity(RespCodeEnum.success);
    }

    /**
     * 删除Privilege
     *
     * @param id 权限主键
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public RespEntity delete(Long id) {
        logger.debug("method[delete] id: " + id);
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[delete] id is null.");
            return new RespEntity(RespCodeEnum.deficiency);
        }
        //判断是否有子级数据
        List<ISysRight> childrenList = sysRightService.findListByParentId(id);
        if (!CollectionUtils.isEmpty(childrenList)) {
            return new RespEntity(RespCodeEnum.error, "请删除该权限下的子权限！");
        }
        //删除 与角色关联数据
        sysRoleService.deleteRightRelByRightId(id);
        //删除权限数据
        sysRightService.deleteById(id);
        return new RespEntity(RespCodeEnum.success);
    }


    /**
     * 获取所有父级Privilege数据
     */
    @ResponseBody
    @RequestMapping(value = "/getParentList", method = RequestMethod.GET)
    public RespEntity getParentList() {
        List<ISysRight> resultList = new ArrayList<>();
        ISysRight systemRight = new SysRight();
        systemRight.setSysRightId(0L);
        systemRight.setSysRightName("无");
        resultList.add(systemRight);
        List<ISysRight> dataList = sysRightService.findParentList(new HashMap());
        resultList.addAll(dataList);
        return new RespEntity(RespCodeEnum.success, resultList);
    }

}  